

#import "LLBaseViewController.h"

@interface LLBaseViewController ()<UIGestureRecognizerDelegate>

@end

@implementation LLBaseViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LLColorRGB(243);
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
    [self prepareData];
    self.navH = LLNavH;
    self.tabH = LLTabBarH;
    self.view.backgroundColor = [UIColor whiteColor];
}


- (void)setNavTitle:(NSString *)navTitle {
    _navTitle = navTitle;
    self.navigationItem.title = navTitle;
}

- (void)setLeftNavTitle:(NSString *)leftNavTitle {
    _leftNavTitle = leftNavTitle;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:leftNavTitle style:UIBarButtonItemStyleDone target:self action:@selector(didClickLeftNavAction)];
}

- (void)setRightNavTitle:(NSString *)rightNavTitle {
    _rightNavTitle = rightNavTitle;
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:rightNavTitle style:UIBarButtonItemStyleDone target:self action:@selector(didClickRightNavAction)];
}

- (void)setRightNavImageName:(NSString *)rightNavImageName {
    _rightNavImageName = rightNavImageName;
    UIImage *image = [[UIImage imageNamed:rightNavImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(didClickRightNavAction)];
}

- (void)setLeftNavImageName:(NSString *)leftNavImageName {
    _leftNavImageName = leftNavImageName;
    UIImage *image = [UIImage imageNamed:leftNavImageName];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(didClickLeftNavAction)];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)prepareData {
    _page = 1;
    _totalPage = 1;
    _pageSize = 10;
}

#pragma mark - 点击事件
- (void)coverBtnClick {
    self.cover.hidden = YES;
}
- (void)forgetPass{
    
}
- (void)payBtnClick:(UIButton *)button{
    
}

#pragma mark - 公共方法
- (void)didClickLeftNavAction {
    [SVProgressHUD dismiss];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didClickRightNavAction {
    
}

- (void)countDown:(UIButton *)btn complete:(countComplete)complete handle:(countingHandle)handle {
    __block NSInteger timeout = 60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (timeout <= 0) {
            // 倒计时结束，关闭
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                if (complete) complete();
            });
        } else {
            NSString *strTime = [NSString stringWithFormat:@"%lds", (long)timeout];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (handle) handle(strTime);
            });
            timeout--;
        }
    });
    dispatch_resume(timer);
}

/**
 *  将普通字符串转换成base64字符串
 *
 *  @param text 普通字符串
 *
 *  @return base64字符串
 */
- (NSString *)base64StringFromText:(NSString *)text {
    
    NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [data base64EncodedStringWithOptions:0];
    
    return base64String;
}


#pragma mark - 字典转json字符串方法
-(NSString *)convertToJsonData:(NSDictionary *)dict{
    NSDictionary *param = dict;
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}

#pragma mark ------ 懒加载
- (NSMutableArray *)models {
    if (_models == nil) {
        _models = [NSMutableArray array];
    }
    return _models;
}
- (UIButton *)cover {
    if (_cover == nil) {
        _cover = [[UIButton alloc] init];
        _cover.backgroundColor = [UIColor blackColor];
        _cover.alpha = 0.4;
        _cover.frame = CGRectMake(0, 0, LLScreenWidth, LLScreenHeight);
        [_cover addTarget:self action:@selector(coverBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_cover];
    }
    return _cover;
}

-(UIView *)titleView{
    if (_titleView == nil) {
        _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, self.navH)];
        _titleView.backgroundColor = LLColor(227, 15, 29);
        [self.view addSubview:_titleView];
        
        _titleLabel = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(0) font:18 textAlignment:NSTextAlignmentCenter];
        _titleLabel.frame = CGRectMake(100, self.navH-33, LLScreenWidth-200, 18);
        _titleLabel.textColor = LLWhiteColor;
        [_titleView addSubview:_titleLabel];
        
        _leftButton = [LLInitBaseViewTool buttonWithTitle:nil image:@"左箭头" titleColor:nil font:0];
        _leftButton.frame = CGRectMake(20, self.navH-33, 28, 18);
        [_leftButton addTarget:self action:@selector(didButtonWithleftButton) forControlEvents:UIControlEventTouchUpInside];
        [self.titleView addSubview:_leftButton];
        
        _rightButton = [LLInitBaseViewTool buttonWithTitle:nil image:nil titleColor:nil font:0];
        _rightButton.frame = CGRectMake(LLScreenWidth-38, self.navH-33, 28, 18);
        [_rightButton addTarget:self action:@selector(didButtonWithrightButton) forControlEvents:UIControlEventTouchUpInside];
        [self.titleView addSubview:_rightButton];
        
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.navH-1, LLScreenWidth, 1)];
        _lineView.backgroundColor = LLColorRGB(235);
        [self.titleView addSubview:_lineView];
    }
    return _titleView;
}

-(void)didButtonWithleftButton{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)didButtonWithrightButton{
    
}

- (NSDate *) getDateFromString:(NSString *)string;
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
    NSDate *date = [formatter dateFromString:string];
    return date;
}

/**
 计算指定时间与当前的时间差
 @param compareDate 某一指定时间
 @return 多少(秒or分or天or月or年)+前 (比如，3天前、10分钟前)
 */
- (NSString *) compareCurrentTime:(NSDate *)compareDate;
{
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];

    timeInterval = -timeInterval;

    long temp = 0;

    NSString *result;

    if (timeInterval < 60) {

        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){

        result = [NSString stringWithFormat:@"%ld分前",temp];
    }
    else if((temp = temp/60) <24){

        result = [NSString stringWithFormat:@"%ld小前",temp];
    }
    else if((temp = temp/24) <30){

        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    else if((temp = temp/30) <12){

        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{

        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }

    return result;
}

- (void)dealloc {
    [LLNotificationCenter removeObserver:self];
}

@end
